console.log("loading: url.utils.js");
var URLUtils = {
	GetParameters : function(link) {
		if (link == undefined) {
			link = window.location.href;
		}
		var paramArr = URLUtils.GetParametersAsString(link).split(/&/);
		var result = {};
		for ( var i = 0; i < paramArr.length; i++) {
			var param = paramArr[i].split(/=/);
			result[param[0]] = param[1];
		}
		return result;
	},
	GetParametersAsString : function(link) {
		if (link == undefined) {
			link = window.location.href;
		}
		var tmp = /\?([^#]*)/.exec(link);
		if (tmp != undefined && tmp[1] != undefined) {
			return tmp[1];
		} else {
			return "";
		}
	},
	GetClearUrl : function(link) {
		if (link == undefined) {
			link = window.location.href;
		}
		return /[^#\?]*/.exec(link);
	},
	SetParameters:function(params,link){
		if (link == undefined) {
			link = window.location.href;
		}
		for(var i in params){
			link = this.SetParameter(i,params[i],link);
		}
		return link;
	},
	SetParametersHref:function(params){
		window.location.href = this.SetParameters(params);
	},
	SetParameter : function(name, value, link) {
		if (link == undefined) {
			link = window.location.href;
		}
		var newValue = name + "=" + value;
		var clearUrl = URLUtils.GetClearUrl(link)+"?";
		var tmp = URLUtils.GetParametersAsString(link);

		var regE = eval("/(\\?|&)" + name + "=([^&]*)/");
		var arr = regE.exec(tmp);
		if (arr != null && arr.length != 0) {
			tmp = tmp.replace(regE, arr[1] + newValue);
		} else if (tmp == undefined || tmp == "" || tmp == "?") {
			tmp = newValue;
		} else {
			tmp = tmp + "&" + newValue;
		}
		return clearUrl + tmp;
	},
	GetBookmarkParameters : function(url) {
		if (url == undefined) {
			url = window.location.href;
		}
		var tmp = /#(.*)/.exec(url);
		if (tmp != undefined && tmp[1] != undefined) {
			var paramArr = tmp[1].split(/\|/);
			var result = {};
			for ( var i = 0; i < paramArr.length; i++) {
				var param = paramArr[i].split(/=/);
				result[param[0]] = param[1];
			}
			return result;
		} else {
			return {};
		}
	},
	GetBookmarkParameter : function(name, defaultValue, url) {
		if (url == undefined) {
			url = window.location.href;
		}
		var tmp = /#.*/.exec(url);
		var regE = eval("/(#|\|)" + name + "=([^|]*)/");
		if (tmp == null || tmp == undefined) {
			return defaultValue;
		} else {
			tmp = tmp[0];
		}
		var tmp = regE.exec(tmp);
		if (tmp == null || tmp == undefined) {
			return defaultValue;
		} else {
			return tmp[2];
		}
	},
	RemoveBookmarkParameter : function(name, url) {
		var _url = url;
		if (url == undefined) {
			url = window.location.href;
		}
		var regE = eval("/(#|\|)" + name + "=([^|]*)/");
		var clearUrl = /[^#]*/.exec(url);
		var tmp = /#.*/.exec(url);
		if (tmp != null && tmp != undefined) {
			tmp = tmp[0];
		}

		var arr = regE.exec(tmp);
		if (arr != null && arr.length != 0) {
			tmp = tmp.replace(regE, "").replace(/\|\|/g, "|");
			if (tmp.length != 0) {
				if (tmp.charAt(0) == "|") {
					tmp = tmp.replace(/\|/, "#");
				} else if (tmp.charAt(0) != "#") {
					tmp = "#" + tmp;
				}
			} else {
				tmp = "#";
			}
		}
		url = clearUrl + tmp;
		if (_url == undefined) {
			window.location = url;
		} else {
			return url;
		}
	},
	SetBookmarkParametersHref:function(params){
		window.location.href = this.SetBookmarkParameters(params);
	},
	SetBookmarkParameters:function(params,link){
		if (link == undefined) {
			link = window.location.href;
		}
		for(var i in params){
			link = this.SetBookmarkParameter(i,params[i],link);
		}
		return link;
	},
	SetBookmarkParameter : function(name, value, url) {
		var newValue = name + "=" + value;
		var _url = url;
		if (url == undefined) {
			url = window.location.href;
		}
		var clearUrl = /[^#]*/.exec(url);
		var tmp = /#.*/.exec(url);
		if (tmp != null && tmp != undefined) {
			tmp = tmp[0];
		}

		var regE = eval("/(#|\|)" + name + "=([^|]*)/");
		var arr = regE.exec(tmp);
		if (arr != null && arr.length != 0) {
			tmp = tmp.replace(regE, arr[1] + newValue);
		} else if (tmp == undefined || tmp == "" || tmp == "#") {
			tmp = "#" + newValue;
		} else {
			tmp = tmp + "|" + newValue;
		}
		url = clearUrl + tmp;
		if (_url == undefined) {
			window.location = url;
		} else {
			return url;
		}
	}
};